<template>
  <div class="app-container">
    <div style="width:400px">
      <el-form
        ref="dataForm"
        :model="temp"
        label-width="120px"

        label-position="right"
      >
        <el-form-item label="原密码">
          <el-input v-model="temp.pwd" placeholder="请输入密码"/>
        </el-form-item>
        <el-form-item label="重置密码">
          <el-input v-model="temp.pwd1" placeholder="请输入密码"/>
        </el-form-item>
        <el-form-item label="再次输入">
          <el-input v-model="temp.pwd2" placeholder="请输入密码"/>
        </el-form-item>
      </el-form>
      <div class="text-right">
        <el-button type="primary" @click="submit">
          确定
        </el-button>
      </div>
    </div>


  </div>
</template>

<script>
import { restpwd } from '@/api/user'
import { removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
const _temp = {
  pwd1: '',
  pwd2: '',
  pwd: ''
}

export default {
  data() {
    return {
      temp: Object.assign({}, _temp)
    }
  },
  created() {

  },
  methods: {
    async logout(){
      await this.$store.dispatch('user/logout')
      this.$router.push(`/login?redirect=${this.$route.fullPath}`)
    },
    submit () {
      if(!this.temp.pwd || !this.temp.pwd1 || !this.temp.pwd2){
        this.$message({
          message: '有必选项没填',
          type: 'error'
        })
      }else{
        if(this.temp.pwd1!=this.temp.pwd2){
          this.$message({
            message: '两次输入的密码不一致',
            type: 'error'
          })
        }else{
          restpwd(this.temp).then(r=>{
            this.$message({
              message: '密码重置成功',
              type: 'success'
            })
            this.temp = Object.assign({}, _temp)
            removeToken() // must remove  token  first
            resetRouter()
            this.logout()
          })
        }
      }
    }
  }
}
</script>
